
from rest_framework import serializers
from django.db import transaction

from api.serializers import ProductListSerializer
from api.models.InventoryModel import WmsReceiptOrder, WmsReceiptOrderDetail, WmsItemSku, WmsItem, WmsInventory


class ItemSkuSerializer(serializers.ModelSerializer):
    class Meta:
        model = WmsItemSku
        fields = ['id', 'sku_name', 'item_id', 'barcode', 'sku_code', 'length', 'width', 'height', 'gross_weight', 'net_weight', 'cost_price', 'selling_price', 'create_by', 'create_time', 'update_by', 'update_time']

class ItemSerializer(serializers.ModelSerializer):
    class Meta:
        model = WmsItem
        fields = ['id', 'item_code', 'item_name', 'item_category', 'unit', 'item_brand', 'remark', 'create_by', 'create_time', 'update_by', 'update_time']


class ReceiptOrderDetailSerializer(serializers.ModelSerializer):
    # 时间的format
    create_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", read_only=True)
    update_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", read_only=True)
    itemSku = serializers.SerializerMethodField()
    item = serializers.SerializerMethodField()
    sku_id = serializers.IntegerField(required=False)
    id = serializers.IntegerField(required=False)

    def get_itemSku(self, obj):
        item_sku = WmsItemSku.objects.filter(id=obj.sku_id).first()
        res = ItemSkuSerializer(instance=item_sku)
        return res.data

    def get_item(self, obj):
        item_sku = WmsItemSku.objects.filter(id=obj.sku_id).first()
        item = WmsItem.objects.filter(id=item_sku.item_id).first()
        res = ItemSerializer(instance=item)
        return res.data

    class Meta:
        model = WmsReceiptOrderDetail
        fields = ['id', 'order_id', 'sku_id', 'quantity', 'amount', 'warehouse_id', 'remark', 'create_by', 'create_time', 'update_by', 'update_time','itemSku','item']

# 前端新增入库单页面选择商品的序列化器自定义返回
class ItemSkuWithReceiptOrderEditSerializer(serializers.ModelSerializer):
    sku_id = serializers.IntegerField(read_only=True,source='id')
    item = serializers.SerializerMethodField()
    itemSku = serializers.SerializerMethodField()

    class Meta:
        model = WmsItemSku
        fields = ['sku_id','item','itemSku']

    def get_itemSku(self, obj):
        item_sku = WmsItemSku.objects.filter(id=obj.id).first()
        res = ItemSkuSerializer(instance=item_sku)
        return res.data

    def get_item(self, obj):
        item_sku = WmsItemSku.objects.filter(id=obj.id).first()
        item = WmsItem.objects.filter(id=item_sku.item_id).first()
        res = ItemSerializer(instance=item)
        return res.data

class ReceiptOrderSerializer(serializers.ModelSerializer):
    # id过长，前端转换时会默认将后几位转为0，所以将id序列化成字符串形式返回
    # 时间的format
    create_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", read_only=True)
    update_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", read_only=True)
    id = serializers.IntegerField(required=False)
    details = ReceiptOrderDetailSerializer(required=False, many=True)

    class Meta:
        model = WmsReceiptOrder
        fields = ['id', 'order_no', 'opt_type', 'merchant_id', 'biz_order_no', 'total_quantity', 'total_amount', 'order_status', 'warehouse_id', 'remark', 'create_by', 'create_time', 'update_by', 'update_time','details']
        read_only_fields = []

    def create(self, validated_data):
        details = validated_data.pop('details')
        receipt_order = WmsReceiptOrder.objects.create(**validated_data)
        for detail in details:
            WmsReceiptOrderDetail.objects.create(order=receipt_order, **detail)
        return receipt_order
    
    def update(self, instance,validated_data):
        details_data = validated_data.pop('details')
        receipt_order = WmsReceiptOrder.objects.filter(id=validated_data.get('id'))
        receipt_order.update(**validated_data)
        for detail in details_data:
            if detail.get('id') is None:
                WmsReceiptOrderDetail.objects.create(order=WmsReceiptOrder.objects.get(id=validated_data.get('id')),**detail)
            else:
                WmsReceiptOrderDetail.objects.filter(id=detail['id']).update(**detail)
        return instance

class BoardListSerializer(serializers.ModelSerializer):
    create_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", read_only=True)
    update_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", read_only=True)
    id = serializers.IntegerField(required=False)
    quantity = serializers.IntegerField(required=True)
    product_id = serializers.IntegerField(required=True)
    product = ProductListSerializer(required=False)
    item = serializers.SerializerMethodField(required=False)
    itemSku = serializers.SerializerMethodField(required=False)

    class Meta:
        model = WmsInventory
        fields = ['id','warehouse_id','product_id','sku_id','quantity', 'remark', 'create_by', 'create_time', 'update_by', 'update_time', 'product','itemSku','item']
        read_only_fields = []

    def get_itemSku(self, obj):
        item_sku = WmsItemSku.objects.get(id=obj.sku_id)
        res = ItemSkuSerializer(instance=item_sku)
        return res.data

    def get_item(self, obj):
        item_sku = WmsItemSku.objects.get(id=obj.sku_id)
        item = WmsItem.objects.get(id=item_sku.item_id)
        res = ItemSerializer(instance=item)
        return res.data
